﻿using Awsc.DataAcquisitionSys.Core.Databases;
using Awsc.DataAcquisitionSys.Core.Services.DataAcquisition;
using MediatR;

namespace Awsc.DataAcquisitionSys.Core.Messages
{
    public class MissionMessageHandler: 
        INotificationHandler<MissionStartMessage>, 
        INotificationHandler<MissionStopMessage>, 
        INotificationHandler<AddSensorMessage>, 
        INotificationHandler<RemoveSensorMessage>,
        INotificationHandler<UpdateSensorMessage>,
        INotificationHandler<AddCollectorMessage>, 
        INotificationHandler<RemoveCollectorMessage>
    {
        private readonly MissionDataWriter missionDataWriter;
        private readonly DataCollectEngine dataCollectEngine;

        public MissionMessageHandler(MissionDataWriter missionDataWriter, DataCollectEngine dataCollectEngine)
        {
            this.missionDataWriter = missionDataWriter;
            this.dataCollectEngine = dataCollectEngine;
        }
        /// <summary>
        /// 开始任务消息处理器
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task Handle(MissionStartMessage notification, CancellationToken cancellationToken)
        {
            try
            {
                missionDataWriter.StartRecording(notification.Mission);
            }
            catch (Exception)
            {
            }


        }
        /// <summary>
        /// 结束任务消息处理器
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Handle(MissionStopMessage notification, CancellationToken cancellationToken)
        {
            try
            {
                missionDataWriter.StopRecording();
            }
            catch (Exception)
            {
            }

        }

        /// <summary>
        /// 添加传感器消息处理器
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Handle(AddSensorMessage notification, CancellationToken cancellationToken)
        {
            try
            {
                await dataCollectEngine.RestartAsync();
                missionDataWriter.AddSensor(notification.Sensor);
            }
            catch (Exception)
            {
            }

        }
        /// <summary>
        /// 更新传感器消息处理器
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Handle(UpdateSensorMessage notification, CancellationToken cancellationToken)
        {
            try
            {
                await dataCollectEngine.RestartAsync();
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// 删除传感器消息处理器
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Handle(RemoveSensorMessage notification, CancellationToken cancellationToken)
        {
            try
            {
                await dataCollectEngine.RestartAsync();
                missionDataWriter.RemoveSensor(notification.Sensor);
            }
            catch (Exception)
            {
            }


        }
        /// <summary>
        /// 添加采集器消息处理器
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Handle(AddCollectorMessage notification, CancellationToken cancellationToken)
        {
            try
            {
                await dataCollectEngine.RestartAsync();
            }
            catch (Exception)
            {
            }
        }


        /// <summary>
        /// 删除采集器消息处理器
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Handle(RemoveCollectorMessage notification, CancellationToken cancellationToken)
        {
            try
            {
                await dataCollectEngine.RestartAsync();
            }
            catch (Exception)
            {
            }
        }

    }
}
